package org.itboys.core.service.mysql.web;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.ExceptionEnum;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.order.SundaySendOrderModelMapper;
import org.itboys.core.dto.order.SundaySendOrderDto;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mysql.order.SundaySendOrder;
import org.itboys.core.entity.mysql.order.SundaySendOrderModel;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.core.service.mysql.front.SundayFrontSendOrderModelService;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/12/28
 * 描述：
 */
@Service
public class SundaySendOrderService extends BaseMySqlService<SundaySendOrder> {
    @Autowired
    private SundaySendOrderModelMapper orderModelMapper;

    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    public PageResult<SundaySendOrder> select(HttpServletRequest request) {
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request, "serviceId", "orderNo", "status", "createTime_start", "createTime_end");
        return new PageResult<>(super.select(param), super.count(param));
    }


    /**
     * 分页获取订单模板
     *
     * @param request
     * @param orderId
     * @return
     */
    public PageResult<SundaySendOrderModel> selectModel(HttpServletRequest request, Long orderId) {
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request, "");
        param.put("orderId", orderId);
        Serializable count = orderModelMapper.count(param);
        Long countResult = count == null ? 0l : Long.valueOf(count.toString());
        return new PageResult<SundaySendOrderModel>(orderModelMapper.select(param), countResult);
    }

    /**
     * 冻结订单
     * @param orderId
     */
    public void freezeOrder(Long orderId) {
        SundaySendOrder order = super.find(orderId);
        if (order.getStatus() != SundayCommonConstants.send_order_status_ydq) {
            throw new ErrorException(ExceptionEnum.SEND_ORDER_STATUS_EXCEPTION.getValue());
        }
        order.setStatus(SundayCommonConstants.send_order_status_ydj);
        super.update(order);
    }

}
